package com.ndp.fb.walle.business.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.ndp.fb.constant.CommonConstants;
import com.ndp.fb.enums.TagForBuildnameType;
import com.ndp.fb.model.business.vo.cache.result.NameAssembly;
import com.ndp.fb.model.business.vo.cache.result.PublishCache;
import com.ndp.fb.model.business.vo.cache.result.TagName;
import com.ndp.fb.rdb.api.TagForBuildnameRdbService;
import com.ndp.fb.rdb.model.TagForBuildnameSeq;
import com.ndp.fb.util.ListUtil;
import com.ndp.fb.util.StringUtil;
import com.ndp.fb.walle.business.TagForBuildnameService;
import com.ndp.fb.walle.model.vo.publishbatch.param.PublishBatchVoParam;
import com.ndp.fb.walle.util.ConversionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by evan.wan on 2015/6/15.
 */
@Component
public class TagForBuildnameServiceImpl implements TagForBuildnameService {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Reference
    private TagForBuildnameRdbService tagForBuildnameRdbService;


    @Override
    public List<TagName> buildTagName(String language)
    {
        logger.info("TagForBuildnameServiceImpl buildTagName() begin.");
        if ("zh-cn".equals(language) || "zh_CN".equals(language) || "zh".equals(language) || "CN".equals(language)) {
            return tagForBuildnameRdbService.getTag();
        } else  {
            return tagForBuildnameRdbService.getTagEN();
        }
    }

    @Override
    public Map<String, String> buildTagVal(List<TagName> tagNames, PublishCache cache)
    {
        logger.info("TagForBuildnameServiceImpl buildTagVal() begin.");
        if(ListUtil.isNotEmpty(tagNames))
        {

            String celltype = "";
            if(ListUtil.isNotEmpty(cache.getAdImages()))
            {
                celltype = "banner";
            }
            if(ListUtil.isNotEmpty(cache.getAdVideos()))
            {
                celltype = "video";
            }
            Map<String, String> defineKey = new HashMap<>();
            //defineKey.put(TagForBuildnameType.COUNTRY.getIndex(), getLocalLocation(buildCountryDescription(localTargetingSpecs.getLocalLocation()), line));
//            defineKey.put(TagForBuildnameType.APPNAME.getIndex() + "", ConversionUtils.replaceConnectionObjectName(cache.getConnectionObject().getName(), "'+|-+|:+|\\s+", "_"));
            //defineKey.put(TagForBuildnameType.gender.getIndex(), getLocalLocation(buildGenderDescription(localTargetingSpecs.getGenders()), line));
            //defineKey.put(TagForBuildnameType.age.getIndex(), getLocalLocation(buildAgeDescription(localTargetingSpecs.getAge()), line));
//            defineKey.put(TagForBuildnameType.PLATFORM.getIndex() + "", cache.getProductConfiguration().getPlatform());
            //defineKey.put(TagForBuildnameType.bidType.getIndex(), "ABSOLUTE_OCPM".equals(shortBidType) ? "ocpm" : shortBidType ); //ocpm
            defineKey.put(TagForBuildnameType.date.getKey() + "", cache.getServerDate().substring(cache.getServerDate().length() - 4, cache.getServerDate().length()));
           defineKey.put(TagForBuildnameType.cell_type.getKey() + "",celltype);
            //defineKey.put(TagForBuildnameType.picturename.getIndex(),getFileNameNoEx(picturename));
            defineKey.put(TagForBuildnameType.creator.getKey() + "", cache.getNickName());
//            if(StringUtil.isNotEmpty(cache.getAdPublishConditionDetailCache().getConnObjSimpleName()))
//                defineKey.put(TagForBuildnameType.APPSIMPLENAME.getIndex() + "", cache.getAdPublishConditionDetailCache().getConnObjSimpleName());
            return defineKey;
        }
        else
            return null;
    }

    @Override
    public void buildSeq(PublishCache cache)
    {
        logger.info("TagForBuildnameServiceImpl buildSeq() begin.");
        TagForBuildnameSeq seq = tagForBuildnameRdbService.getSeq(cache.getUserId(), cache.getConnectionObject().getFbConnectionObjectId(), cache.getProductConfiguration().getId());
        if(seq != null)
        {
            String connObjSimpleName = cache.getAdPublishConditionDetailCache().getConnObjSimpleName();
            //营销活动名称 曾经记录
            List<NameAssembly> adCampSeq = JSON.parseArray(seq.getAdCampSeq(), NameAssembly.class);
            if(ListUtil.isNotEmpty(adCampSeq))
            {
                cache.setAdCampSeq(buildSeq(adCampSeq, connObjSimpleName));
            }

            //广告组名称 曾经记录
            List<NameAssembly> adSetSeq = JSON.parseArray(seq.getAdSetSeq(), NameAssembly.class);
            if(ListUtil.isNotEmpty(adSetSeq))
            {
                cache.setAdSetSeq(buildSeq(adSetSeq, connObjSimpleName));
            }

            //广告名称 曾经记录
            List<NameAssembly> adGroupSeq = JSON.parseArray(seq.getAdGroupSeq(), NameAssembly.class);
            if(ListUtil.isNotEmpty(adGroupSeq))
            {
                cache.setAdGroupSeq(buildSeq(adGroupSeq, connObjSimpleName));
            }
       }

        if (StringUtils.equals(CommonConstants.OUTLINK, cache.getConnectionObject().getProductType())){
            cache.setAdGroupSeq(buildOutlinkAdgroupSeq());
        }
    }

    private List<NameAssembly> buildOutlinkAdgroupSeq() {
        List<NameAssembly> result = new ArrayList<>();
        result.add(new NameAssembly(TagForBuildnameType.short_date.getKey(), "短日期", "tag"));
        result.add(new NameAssembly(TagForBuildnameType.lite_serial_number.getKey(), "小流水", "tag"));
        return result;
    }

    private List<NameAssembly> buildSeq(List<NameAssembly> adCampSeq, String connObjSimpleName) {
        List<NameAssembly> result = new ArrayList<>();
        for(NameAssembly nameAssembly : adCampSeq)
        {
//            if(nameAssembly.getTag() == 11 && StringUtil.isEmpty(connObjSimpleName))
//                continue;
            result.add(nameAssembly);
        }

        return result;
    }

    @Override
    public void saveSeq(String userId, Long fbConnObjId, Long productCfgId, PublishBatchVoParam publishBatchVoParam)
    {
        logger.info("TagForBuildnameServiceImpl saveSeq() begin.");
        TagForBuildnameSeq seq = new TagForBuildnameSeq();
        seq.setUserId(userId);
        seq.setFbConnObjId(fbConnObjId);
        seq.setProductCfgId(productCfgId);
        seq.setAdCampSeq(publishBatchVoParam.getAdCampSeq());
        seq.setAdSetSeq(publishBatchVoParam.getAdSetSeq());
        seq.setAdGroupSeq(publishBatchVoParam.getAdGroupSeq());
        tagForBuildnameRdbService.saveSeq(seq);
    }

}
